home *** CD-ROM | disk | FTP | other *** search
- Path: sn.no!usenet
- From: jan-henrik.haukeland@fou.telenor.no (jan-henrik haukeland)
- Newsgroups: comp.lang.c
- Subject: Re: Complete Newbie Question
- Supersedes: <m220oavowh.fsf@hawk.no>
- Date: 04 Feb 1996 21:26:48 +0100
- Organization: LH*281263
- Message-ID: <m2zqayu99z.fsf_-_@hawk.no>
- References: <4f0ibf$2pe@mars.dsu.edu>
- NNTP-Posting-Host: nm6-ppp14.oslo.net
- In-reply-to: scotty@fpg.gcomm.com's message of 3 Feb 1996 20:59:27 GMT
- X-Newsreader: Gnus v5.0.12
-
- Scotty writes:
-
- : I'm just learning C, and for the life of me I can't find anywhere
- : how to make the printf statements print in the Upper left corner of the
- : screen, nor how to clear the screen. If you could help me, please do! ;)
-
- Here is a `hack' using escape sequences to do the screen stuff Your
- asking for. But, if Your on Unix consider to use ncurses instead, on
- dos something similar .
-
- The following are from an excellent article by Matt Weisfeld in one of
- last Years DDJ:
-
- jhh
-
- -----<snip>------------------------------------------------------------------
- /***************************************************
-
- FILE NAME : scrlibs.h
- AUTHOR : Matt Weisfeld
-
- DESCRIPTION : header file for scrlibs.c
-
- ***************************************************/
- #ifdef VMS
- #define ANSI_COMPILER
- #define ANSI_SEQUENCES
- #define VT100
- #endif
-
- #ifdef SLC
- #define UNIX
- #define K_R_COMPILER
- #define ANSI_SEQUENCES
- #define VT100
- #endif
-
- #ifdef BCC
- #define DOS
- #define ANSI_COMPILER
- #define ANSI_SEQUENCES
- #endif
-
- #ifdef HPUX
- #define UNIX
- #define ANSI_COMPILER
- #define HPUX_SEQUENCES
- #endif
-
- #ifdef ANSI_COMPILER
-
- void do_bold(FILE *);
- void do_normal(FILE *);
- void do_blink(FILE *);
- void do_reverse(FILE *);
-
- void cursor_right(int);
- void cursor_left(int);
- void cursor_down(int);
- void cursor_up(int);
- void cursor_pos(int, int);
- void cursor_home(void);
-
- void erase_display(void);
-
- #else
-
- void do_bold();
- void do_normal();
- void do_blink();
- void do_reverse();
-
- void cursor_right();
- void cursor_left();
- void cursor_down();
- void cursor_up();
- void cursor_pos();
- void cursor_home();
- void cursor_bottom();
-
- void erase_display();
-
- #endif
-
- #ifdef ANSI_SEQUENCES
-
- #ifdef DOS
- static char bold[] = {"\033[36;1m"};
- #else
- static char bold[] = {"\033[1m"};
- #endif
- static char normal[] = {"\033[0m"};
- static char blink[] = {"\033[5m"};
- static char reverse[] = {"\033[7m"};
-
- static char curright[] = {"\033[%dC"};
- static char curleft[] = {"\033[%dD"};
- static char curdown[] = {"\033[%dB"};
- static char curup[] = {"\033[%dA"};
- static char curpos[] = {"\033[%d;%dH"};
- static char erasedisp[] = {"\033[2J"};
-
- #endif
-
- #ifdef HPUX_SEQUENCES
-
- static char bold[] = {"\033&dH"};
- static char normal[] = {"\033&d@"};
- static char blink[] = {"\033&dA"};
- static char reverse[] = {"\033&dK"};
- static char underline[] = {"\033&dD"};
-
- static char curfor[] = {"\033&a+%dC"};
- static char curback[] = {"\033&a-%dC"};
- static char curdown[] = {"\033&a+%dR"};
- static char curup[] = {"\033&a-%dR"};
- static char curpos[] = {"\033&a%dx%dY"};
-
- static char erasedisp[] = {"\033J"};
-
- #endif
-
- -----<snip>------------------------------------------------------------------
- /***************************************************
-
- FILE NAME : scrlibs.c
- AUTHOR : Matt Weisfeld
-
- DESCRIPTION : screen handling libraries
-
- ***************************************************/
- #include <stdio.h>
- #include "scrlibs.h"
-
- void do_bold(stream)
- FILE *stream;
- {
- fprintf (stream, bold);
- fflush(stream);
- return;
- }
-
- void do_normal(stream)
- FILE *stream;
- {
- fprintf (stream, normal);
- fflush(stream);
- return;
- }
-
- void do_blink(stream)
- FILE *stream;
- {
- fprintf (stream, blink);
- fflush(stdout);
- return;
- }
-
- void do_reverse(stream)
- FILE *stream;
- {
- fprintf (stream, reverse);
- fflush(stdout);
- return;
- }
-
- void cursor_right(move)
- int move;
- {
- int i;
-
- printf (curright, move);
- fflush(stdout);
- return;
- }
-
- void cursor_left(move)
- int move;
- {
- int i;
-
- printf (curleft, move);
- fflush(stdout);
- return;
- }
-
- void cursor_down(move)
- int move;
- {
- int i;
-
- printf (curdown, move);
- fflush(stdout);
- return;
- }
-
- void cursor_up(move)
- int move;
- {
- int i;
-
- printf (curup, move);
-
- fflush(stdout);
- return;
- }
-
- void cursor_pos(row,col)
- int row,col;
- {
-
- #ifdef HPUX
- printf (curpos, col,row);
- #else
- printf (curpos, row,col);
- #endif
- fflush(stdout);
- return;
- }
-
- void cursor_home()
- {
- cursor_pos(0,0);
- fflush(stdout);
- return;
- }
-
- void cursor_bottom()
- {
- cursor_pos(23,0);
- fflush(stdout);
- return;
- }
-
- void erase_display()
- {
- int i;
-
- cursor_home();
- printf (erasedisp);
- fflush(stdout);
- return;
- }
-
-
- --<snip>----------------------------------------------------------------------
- /*
- * My own Homemade KeyPressed routine: Wait for a User to press a key.
- */
- void KeyPressed ()
- {
- int c;
- struct termio tio, tin;
-
- ioctl (0, TCGETA, &tio);
-
- tin = tio;
- tin.c_lflag &= ~ECHO; /* echo off */
- tin.c_lflag &= ~ICANON; /* ICANON off */
- /*
- * Emulate cbreak mode
- */
- tin.c_cc[VMIN] = 1;
- tin.c_cc[VTIME] = 0;
-
- /*
- * Set cbreak-mode
- */
- ioctl (0, TCSETA, &tin);
-
- /* Read a char and reset the tty */
- c = getchar ();
-
- ioctl(0, TCSETA, &tio);
-
- /* A alternative for lazy programmers */
- /* system ("/bin/stty cbreak"); */
- /* c = getchar (); */
- /* system ("/bin/stty -cbreak"); */
- return;
- }
-